import "reflect-metadata";
import { InversifyExpressServer } from "inversify-express-utils";
import { Container } from "inversify";
import express from "express";
import { UserController } from "./src/user/controller";
import { UserService } from "./src/user/service";
import { PrismaDB } from "./db";
import { JWT } from "./utils/jwt";

const container = new Container();

// 单实例绑定，整个应用只有一个实例
container.bind(PrismaDB).toSelf().inSingletonScope();

// 绑定到自身，每次请求都会创建一个新的实例
container.bind(UserController).to(UserController);
container.bind(UserService).to(UserService);
container.bind(JWT).to(JWT);

const server = new InversifyExpressServer(container);
// 配置中间件,允许post参数
server.setConfig((app) => {
  app.use(express.json());
  app.use(container.get(JWT).init()); //主要代码
});

const app = server.build();

app.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});
